//package com.qulai.amazon_us.Interceptor;
//
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.mapping.BoundSql;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.mapping.ParameterMapping;
//import org.apache.ibatis.mapping.SqlSource;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.apache.ibatis.session.ResultHandler;
//import org.apache.ibatis.session.RowBounds;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Map;
//import java.util.regex.Pattern;
//
//@Intercepts({
//        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
//        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
//})
//@Component
//public class DynamicTableInterceptor implements Interceptor {
//
//    private static final String TABLE_PLACEHOLDER = "${dynamicTable}";
//    private static final Pattern VALID_TABLE_NAME = Pattern.compile("^record_upload_[a-zA-Z0-9_]{1,20}$");
//
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        Object[] args = invocation.getArgs();
//        MappedStatement ms = (MappedStatement) args[0];
//        Object parameter = args[1];
//
//        // 从参数中获取表名
//        String dynamicTable = extractTableName(parameter);
//        if (dynamicTable == null || !isValidTableName(dynamicTable)) {
//            return invocation.proceed();
//        }
//
//        // 获取原始SQL
//        BoundSql boundSql = ms.getBoundSql(parameter);
//        String sql = boundSql.getSql();
//
//        // 替换表名占位符
//        sql = sql.replace(TABLE_PLACEHOLDER, escapeSql(dynamicTable));
//
//        // 创建新的BoundSql
//        BoundSql newBoundSql = createNewBoundSql(ms, boundSql, sql);
//
//        // 创建新的MappedStatement
//        MappedStatement newMs = createNewMappedStatement(ms, newBoundSql);
//        args[0] = newMs;
//
//        return invocation.proceed();
//    }
//
//    private String extractTableName(Object parameter) {
//        if (parameter instanceof Map) {
//            Map<?, ?> paramMap = (Map<?, ?>) parameter;
//            Object tableNameObj = paramMap.get("dynamicTable");
//            return tableNameObj != null ? tableNameObj.toString() : null;
//        }
//        return null;
//    }
//
//    private boolean isValidTableName(String tableName) {
//        return tableName != null && VALID_TABLE_NAME.matcher(tableName).matches();
//    }
//
//    private String escapeSql(String tableName) {
//        return "`" + tableName.replace("`", "") + "`";
//    }
//
//    private BoundSql createNewBoundSql(MappedStatement ms, BoundSql boundSql, String sql) {
//        // 复制原始BoundSql的参数映射
//        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
//
//        // 创建新的BoundSql
//        BoundSql newBoundSql = new BoundSql(
//                ms.getConfiguration(),
//                sql,
//                parameterMappings,
//                boundSql.getParameterObject()
//        );
//
//        // 复制原始BoundSql的附加参数
//        boundSql.getAdditionalParameters().forEach(newBoundSql::setAdditionalParameter);
//
//        return newBoundSql;
//    }
//
//    private MappedStatement createNewMappedStatement(MappedStatement ms, BoundSql newBoundSql) {
//        MappedStatement.Builder builder = new MappedStatement.Builder(
//                ms.getConfiguration(),
//                ms.getId(),
//                new BoundSqlSqlSource(newBoundSql),
//                ms.getSqlCommandType()
//        );
//
//        // 复制原始MappedStatement的属性
//        builder.resource(ms.getResource());
//        builder.fetchSize(ms.getFetchSize());
//        builder.statementType(ms.getStatementType());
//        builder.keyGenerator(ms.getKeyGenerator());
//        if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
//            builder.keyProperty(String.join(",", ms.getKeyProperties()));
//        }
//        if (ms.getKeyColumns() != null && ms.getKeyColumns().length > 0) {
//            builder.keyColumn(String.join(",", ms.getKeyColumns()));
//        }
//        builder.timeout(ms.getTimeout());
//        builder.parameterMap(ms.getParameterMap());
//        builder.resultMaps(ms.getResultMaps());
//        builder.resultSetType(ms.getResultSetType());
//        builder.cache(ms.getCache());
//        builder.flushCacheRequired(ms.isFlushCacheRequired());
//        builder.useCache(ms.isUseCache());
//        builder.databaseId(ms.getDatabaseId());
//
//        return builder.build();
//    }
//
//    private static class BoundSqlSqlSource implements SqlSource {
//        private final BoundSql boundSql;
//
//        public BoundSqlSqlSource(BoundSql boundSql) {
//            this.boundSql = boundSql;
//        }
//
//        @Override
//        public BoundSql getBoundSql(Object parameterObject) {
//            return boundSql;
//        }
//    }
//}